package com.sino.androiddesign.main

import android.content.Context
import android.content.Intent
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import com.sino.androiddesign.R
import com.sino.androiddesign.anim.AnimActivity
import com.sino.androiddesign.common.ItemClickListener
import com.sino.androiddesign.customView.CustomViewActivity
import com.sino.androiddesign.database.DataBaseActivity
import com.sino.androiddesign.design.DesignActivity
import com.sino.androiddesign.network.NetWorkActivity
import com.sino.androiddesign.recyclerviewdemo.RecyclerActivity
import com.sino.androiddesign.test.TestActivity
import kotlinx.android.synthetic.main.activity_main.*

/**
 * 主界面  包含了 几个designView 的列表
 */
class MainActivity : AppCompatActivity(), ItemClickListener {

    override fun clickItemListener(position: Int) {
        when (position) {
            0 -> {
                startActivity(Intent(this, RecyclerActivity::class.java))
            }
            1 -> {
                startActivity(Intent(this, DesignActivity::class.java))
            }
            2 -> {
                startActivity(Intent(this, AnimActivity::class.java))
            }
            3 -> {
                startActivity(Intent(this, DataBaseActivity::class.java))
            }
            4 -> {

                startActivity(Intent(this, CustomViewActivity::class.java))
            }

            6 -> {
                startActivity(Intent(this, NetWorkActivity::class.java))
            }
            7 -> {
                startActivity(Intent(this, TestActivity::class.java))
            }
        }
    }


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        initToolbar()
        initRecyclerView()
    }


    private fun initRecyclerView() {

        val datas: ArrayList<String> = ArrayList<String>()
        init(datas)
        val mainAdapter: MainAdapter = MainAdapter(datas, this, this)
        main_rv.layoutManager = LinearLayoutManager(this)
        main_rv.adapter = mainAdapter
    }

    /**
     * todo  练习并总结
     */
    private fun init(datas: ArrayList<String>) {
        datas.add("列表View: RecyclerView")
        datas.add("Design包控件")
        datas.add("android动画") //重点属性动画
        datas.add("数据库")  // 原生SQLite  GreenDao  ObjectBox
        datas.add("练习自定义View")
        datas.add("网络请求")   //retrofit
        datas.add("第三方分享")
        datas.add("第三方支付")
        datas.add("测试信息")
    }

    private fun initToolbar() {
        main_toolbar.title = ""
        setSupportActionBar(main_toolbar)
    }
}

class MainAdapter(var data: List<String>, private var mContext: Context, private var listener: ItemClickListener) : RecyclerView.Adapter<MainViewHolder>() {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MainViewHolder {
        var rootView = LayoutInflater.from(mContext).inflate(R.layout.main_item_layout, parent, false)
        var viewHolder = MainViewHolder(rootView)
        viewHolder.bindView(rootView)
        return viewHolder
    }

    override fun getItemCount(): Int {
        return data.size
    }

    override fun onBindViewHolder(holder: MainViewHolder, position: Int) {

        holder.textTv!!.text = data[position]
        holder.itemView.setOnClickListener {
            listener.clickItemListener(position)
        }
    }
}

class MainViewHolder(itemView: View?) : RecyclerView.ViewHolder(itemView) {
    var textTv: TextView? = null


    fun bindView(itemView: View?) {
        textTv = itemView!!.findViewById(R.id.main_item_text_tv)
    }
}
